//
//  SINAStuActivityAddTargetViewController.m
//  sinashebao
//
//  Created by  androidlongs on 2018/1/23.
//  Copyright © 2018年  androidlongs. All rights reserved.
//

#import "SINAStuActivityAddTargetViewController.h"
#import "SINACircularBeadUtils.h"
#import "SINACalendarUtils.h"
#import "SINAStuActivityAddTargetInputViewController.h"
#import "SINAUiviewClickLiserner.h"
#import "SINAStuActivityTargetModel.h"
#import "SINACalendarUtils.h"

@interface SINAStuActivityAddTargetViewController ()
//时间白色背景
@property (weak, nonatomic) IBOutlet UIView *timeWhiteBackGroundView;
//时间蓝色背景
@property (weak, nonatomic) IBOutlet UIImageView *timeBlueBackGroundView;
//时间显示
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel2;
@property (strong, nonatomic) NSTimer *timer;

@property (strong, nonatomic) NSMutableArray *targetArray;
//开始按钮
@property (weak, nonatomic) IBOutlet UILabel *startTimeLabel;
@property(nonatomic) BOOL isTimeShow;

@property(nonatomic) BOOL isLoadingShow;

@property(nonatomic,assign)BOOL isUpdateItemLocation;

//添加小目标
@property (weak, nonatomic) IBOutlet UIView *addtargetUiview;
//小目标窗口
@property (weak, nonatomic) IBOutlet UIView *targetUiview;

@property(strong,nonatomic)CABasicAnimation *timeAnimation;

//时间的时间
@property(strong,nonatomic) NSString *format_time;

//当前选中的未开始的小目标
@property(strong,nonatomic) SINAStuActivityTargetModel *targetModel;

@end

@implementation SINAStuActivityAddTargetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title=@"小目标";
    self.isUpdateItemLocation=NO;
    self.isLoadingShow=YES;
    
    self.targetArray =[[NSMutableArray alloc]init];
    
    [self initViewFunction];
    [self timeFunction];
    
    __weak typeof(self) weakSelf = self;
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.addtargetUiview andWithBlick:^(UIView *view) {
        
        if (weakSelf.targetArray.count<3) {
            SINAStuActivityAddTargetInputViewController *inputController = [[SINAStuActivityAddTargetInputViewController alloc] initWithNibName:@"SINAStuActivityAddTargetInputViewController" bundle:nil];
            inputController .model  =  weakSelf.model;
            [weakSelf.navigationController pushViewController:inputController animated:YES];
        }
        
    }];
    
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.startTimeLabel andWithBlick:^(UIView *view) {
        [weakSelf startTargetFunction];
    }];
    CABasicAnimation *animation =  [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    //默认是顺时针效果，若将fromValue和toValue的值互换，则为逆时针效果
    animation.fromValue = [NSNumber numberWithFloat:0.f];
    animation.toValue =  [NSNumber numberWithFloat: M_PI *2];
    animation.duration  = 60;
    animation.autoreverses = NO;
    animation.removedOnCompletion = NO;
    animation.fillMode =kCAFillModeForwards;
    //如果这里想设置成一直自旋转，可以设置为MAXFLOAT，否则设置具体的数值则代表执行多少次
    animation.repeatCount = MAXFLOAT;
    self.timeAnimation=animation;
    [self.timeBlueBackGroundView.layer addAnimation:animation forKey:nil];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(noticLoadingTarGetDataFunction) name:@"addTargetNotification" object:nil];
    
}
-(void)viewWillAppear:(BOOL)animated{
    [self pauseAnimation];
    [self resumeAnimation];
    [self loadingTarGetDataFunction];
}
-(void)viewDidDisappear:(BOOL)animated{
    [self pauseAnimation];
}
-(void) initViewFunction{
    [SINACircularBeadUtils makeCircularView:self.timeWhiteBackGroundView];
    [SINACircularBeadUtils makeCircularView:self.startTimeLabel :20];
    [SINACircularBeadUtils makeCircularBead:1 andView:self.addtargetUiview andIsSetBackground:NO];
    
    //    __weak typeof(self) weakSelf = self;
    NSMutableArray *targetArray =self.model.teamTargetArray;
    self.targetArray =targetArray;
    if (self.targetArray==nil) {
        self.targetArray =[[NSMutableArray alloc]init];
    }
    [self addTargetViewFunction];
    
    
    
}
-(void) timeFunction{
    self.format_time =[[SINACalendarUtils alloc]getCurrentTime:@"HH:mm"];
    __weak typeof(self) weakSelf = self;
    if (@available(iOS 10.0, *)) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
            
            if (weakSelf.isTimeShow) {
                weakSelf.timeLabel.text=weakSelf.format_time;
                weakSelf.timeLabel2.hidden=YES;
                
            } else {
                weakSelf.timeLabel.text=weakSelf.format_time;
                weakSelf.timeLabel2.hidden=NO;
            }
            weakSelf.isTimeShow=!weakSelf.isTimeShow;
            if (self.targetModel!=nil) {
                NSString *currentTime =[SINACalendarUtils currentTimeStr];
                NSString *str_hour=@"";
                NSString *str_minute=@"";
                NSInteger seconds=0;
                if (self.targetModel.startTime ==0) {
                    str_hour=@"00";
                    str_minute=@"00";
                }else{
                    seconds = ([currentTime integerValue]-self.targetModel.startTime)/1000;
                    
                    //format of hour
                    str_hour = [NSString stringWithFormat:@"%02ld",seconds/3600];
                    //format of minute
                    str_minute = [NSString stringWithFormat:@"%02ld",(seconds%3600)/60];
                    //format of second
                    //            NSString *str_second = [NSString stringWithFormat:@"%02ld",seconds%60];
                    //format of time
                }
                
                NSString *format_time = [NSString stringWithFormat:@"%@:%@",str_hour,str_minute];
                weakSelf.timeLabel.text=format_time;
            }
            
            
        }];
    } else {
        // Fallback on earlier versions
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(repeatTimeFunction) userInfo:nil repeats:true];
            [[NSRunLoop currentRunLoop] run];
            
        });
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//暂停动画

- (void)pauseAnimation {
    //获取当前暂停时间
    //CFTimeInterval pauseTime = [self.subLayer convertTime:CACurrentMediaTime() fromLayer:nil];
    CFTimeInterval pauseTime = CACurrentMediaTime();
    
    //层的速度为0,停止动画
    self.timeBlueBackGroundView.layer.speed = 0;
    
    //保存暂停时间,便于恢复
    self.timeBlueBackGroundView.layer.timeOffset = pauseTime;
}

//恢复动画
- (void)resumeAnimation {
    
    //获取暂停时保存的时间
    CFTimeInterval pauseTime = self.timeBlueBackGroundView.layer.timeOffset;
    self.timeBlueBackGroundView.layer.timeOffset = 0;
    
    //设置速度
    self.timeBlueBackGroundView.layer.speed = 1.0;
    //清除开始时间
    self.timeBlueBackGroundView.layer.beginTime = 0.0;
    //计算开始时间
    CFTimeInterval beginTime = [self.timeBlueBackGroundView.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pauseTime;
    //重设开始时间
    self.timeBlueBackGroundView.layer.beginTime = beginTime;
}
//开启小目标
-(void) startTargetFunction{
    if (self.targetModel==nil) {
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"请选择小目标" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
    } else {
        //开启 1开始  2结束
        //        NSString *startTiime = self.targetModel.startTime;
        NSInteger flag = self.targetModel.targetFlag;
        if (flag==0) {
            [self updateTargetTime:1];
        }else  if (flag==1) {
            [self updateTargetTime:2];
        }else{
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"小目标已完成" sureBtn:@"确定"
                                                                 cancleBtn:nil];
            [alertView showXLAlertView];
        }
    }
}
-(void)noticLoadingTarGetDataFunction{
    self.isLoadingShow =NO;
    //[self noticLoadingTarGetDataFunction];
}
-(void)addTargetViewFunction{
    [self.targetUiview removeAllSubviews];
    __weak typeof(self) weakSelf = self;
    if (self.targetArray==nil||self.targetArray.count==0) {
        self.targetUiview.hidden=YES;
        
    } else {
        self.targetUiview.hidden=NO;
        self.targetUiview.backgroundColor=[UIColor clearColor];
        NSInteger itemWidth=mainW-30;
        for (NSInteger i=0; i<self.targetArray.count; i++) {
            
            SINAStuActivityTargetModel *targetModel = self.targetArray[i];
            
            
            //下滑删除
            UIView *deleteItemView =[[UIView alloc]initWithFrame:CGRectMake(0, i*63,itemWidth , 63)];
            deleteItemView.backgroundColor=[UIColor redColor];
            deleteItemView.tag=2001;
            
            if (i==0) {
                deleteItemView.frame=CGRectMake(0, -4,itemWidth , 63);
            } else {
                deleteItemView.frame=CGRectMake(0, i*63-4,itemWidth , 63);
            }
            [self.targetUiview addSubview:deleteItemView];
            
            UILabel *deleteLabel = [[UILabel alloc]init];
            deleteLabel.tag=2002;
            deleteLabel.frame=CGRectMake(CGRectGetWidth(deleteItemView.frame)-100, 0,100 , CGRectGetHeight(deleteItemView.frame));
            deleteLabel.text=@"删除";
            deleteLabel.textAlignment = NSTextAlignmentCenter;
            deleteLabel.textColor = [UIColor whiteColor];
            [deleteItemView addSubview:deleteLabel];
            
            
            
            //小目标显示View
            UIView *itemView =[[UIView alloc]initWithFrame:CGRectMake(0, i*63,itemWidth , 63)];
            itemView.backgroundColor=[UIColor whiteColor];
            if (i==0) {
                itemView.frame=CGRectMake(0, -4,itemWidth , 63);
            } else {
                itemView.frame=CGRectMake(0, i*63-4,itemWidth , 63);
            }
            
            
            
            
            NSString *title=targetModel.title;
            if (title==nil||title.length==0||[title isEqualToString:@"(null)"]) {
                title=@"  ***";
            }
            
            UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(15,10,itemWidth, 21)];
            titleLabel.text=[NSString stringWithFormat:@"%ld %@",i+1,title];
            titleLabel.font=[UIFont systemFontOfSize:16];
            titleLabel.numberOfLines=0;
            titleLabel.textColor=[UIColor colorWithHexString:@"#2e2e2e"];
            titleLabel.tag=1001;
            [itemView addSubview:titleLabel];
            
            UILabel *timeLabel=[[UILabel alloc]initWithFrame:CGRectMake(15,CGRectGetMaxY(titleLabel.frame)+4,itemWidth, 21)];
            timeLabel.text=@"用时20分钟";
            timeLabel.font=[UIFont systemFontOfSize:14];
            timeLabel.numberOfLines=0;
            timeLabel.textColor=[UIColor colorWithHexString:@"#878787"];
            timeLabel.tag=1002;
            
            [itemView addSubview:timeLabel];
            
            
            NSInteger startTime=targetModel.startTime;
            if (startTime==0) {
                timeLabel.text=@"未开始";
            } else {
                NSString *currentTime =[SINACalendarUtils currentTimeStr];
                NSInteger seconds = ([currentTime integerValue]-startTime)/1000;
                //format of hour
                NSString *str_hour = [NSString stringWithFormat:@"%02ld",seconds/3600];
                //format of minute
                NSString *str_minute = [NSString stringWithFormat:@"%02ld",(seconds%3600)/60];
                //format of second
                //                NSString *str_second = [NSString stringWithFormat:@"%02ld",seconds%60];
                //format of time
                NSString *format_time = [NSString stringWithFormat:@"用时%@时%@分",str_hour,str_minute];
                timeLabel.text=format_time;
            }
            
            
            
            
            if (i<self.targetArray.count-1) {
                UIView *diviView =[[UIView alloc]initWithFrame:CGRectMake(15, 62, itemWidth-30, 1)];
                diviView.backgroundColor=[UIColor colorWithHexString:@"#eeeeee"];
                diviView.tag=1003;
                [itemView addSubview:diviView];
            }
            if (i==0) {
                UIView *diviView =[[UIView alloc]initWithFrame:CGRectMake(15, 0, itemWidth, 1)];
                diviView.backgroundColor=[UIColor colorWithHexString:@"#eeeeee"];
                diviView.tag=1004;
                //[itemView addSubview:diviView];
            }
            
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(itemWidth-33,21-6 , 18, 18)];
            imageView.backgroundColor=[UIColor colorWithHexString:@"#eeeeee"];
            [SINACircularBeadUtils makeCircularView:imageView];
            imageView.tag=1005;
            
            [itemView addSubview:imageView];
            
            itemView.tag=100+i;
            
            [[SINAUiviewClickLiserner alloc]viewClickFunction:itemView andWithBlick:^(UIView *view) {
                
                NSInteger index = itemView.tag-100;
                SINAStuActivityTargetModel *targetModel = self.targetArray[index];
                weakSelf.targetModel = targetModel;
                NSString *format_time=@"00:00";
                if (targetModel.startTime!=0) {
                    
                    //开始时间不为空 已开始
                    targetModel.targetFlag=1;
                    
                    NSString *currentTime =[SINACalendarUtils currentTimeStr];
                    NSInteger seconds = ([currentTime integerValue]-targetModel.startTime)/1000;
                    
                    //format of hour
                    NSString *str_hour = [NSString stringWithFormat:@"%02ld",seconds/3600];
                    //format of minute
                    NSString *str_minute = [NSString stringWithFormat:@"%02ld",(seconds%3600)/60];
                    //format of second
                    //                    NSString *str_second = [NSString stringWithFormat:@"%02ld",seconds%60];
                    //format of time
                    format_time = [NSString stringWithFormat:@"%@:%@",str_hour,str_minute];
                    weakSelf.startTimeLabel.textColor=[UIColor colorWithHexString:@"#eeeeee"];
                    weakSelf.startTimeLabel.textColor=baseSelectColor;
                    if (targetModel.endTime!=nil&&targetModel.endTime !=0){
                        //开始时间不为空 已开始
                        //并且结束时间不为空 已结束
                        targetModel.targetFlag=2;
                        weakSelf.startTimeLabel.textColor=[UIColor colorWithHexString:@"#878787"];
                    }
                    weakSelf.startTimeLabel.text=@"END";
                    [weakSelf pauseAnimation];
                    [weakSelf resumeAnimation];
                    
                } else {
                    weakSelf.targetModel=targetModel;
                    [weakSelf pauseAnimation];
                    weakSelf.startTimeLabel.textColor=baseSelectColor;
                    weakSelf.startTimeLabel.text=@"START";
                    format_time=@"00:00";
                }
                weakSelf.format_time=format_time;
                
                for (NSInteger i=0; i<self.targetUiview.subviews.count; i++) {
                    UIView *flagView =self.targetUiview.subviews[i];
                    NSInteger flagTag = flagView.tag;
                    
                    if (flagView.tag!=itemView.tag&&flagTag!=2001&&flagTag!=2002) {
                        flagView.backgroundColor=[UIColor whiteColor];
                        for (NSInteger j=0; j<flagView.subviews.count; j++) {
                            UIView *ifagView =flagView.subviews[j];
                            if (ifagView.tag==1003||ifagView.tag==1004) {
                                ifagView.backgroundColor=[UIColor colorWithHexString:@"#eeeeee"];
                            }else if (ifagView.tag==1002) {
                                ifagView.backgroundColor=[UIColor clearColor];;
                                ((UILabel*)ifagView).textColor=[UIColor colorWithHexString:@"#878787"];
                                SINAStuActivityTargetModel *targetModel = self.targetArray[i/2];
                                NSString *format_time=@"00:00";
                                if (targetModel.startTime!=nil&&targetModel.startTime !=0) {
                                    NSString *currentTime =[SINACalendarUtils currentTimeStr];
                                    NSInteger seconds = ([currentTime integerValue]-targetModel.startTime )/1000;
                                    
                                    //format of hour
                                    NSString *str_hour = [NSString stringWithFormat:@"%02ld",seconds/3600];
                                    //format of minute
                                    NSString *str_minute = [NSString stringWithFormat:@"%02ld",(seconds%3600)/60];
                                    //format of second
                                    //                                    NSString *str_second = [NSString stringWithFormat:@"%02ld",seconds%60];
                                    //format of time
                                    format_time = [NSString stringWithFormat:@"用时%@时%@分",str_hour,str_minute];
                                    ((UILabel*)ifagView).text=format_time;
                                }
                            }else if (ifagView.tag==1001) {
                                ifagView.backgroundColor=[UIColor clearColor];;
                                ((UILabel*)ifagView).textColor=[UIColor colorWithHexString:@"#2e2e2e"];
                                //                                SINAStuActivityTargetModel *targetModel = self.targetArray[i];
                                
                            }else if (ifagView.tag==1005) {
                                ifagView.backgroundColor=[UIColor colorWithHexString:@"#eeeeee"];;
                                ((UIImageView*)ifagView).image=nil;
                            }
                        }
                    }else if(flagTag!=2001&&flagTag!=2002){
                        flagView.backgroundColor=baseSelectColor;
                        for (NSInteger j=0; j<flagView.subviews.count; j++) {
                            UIView *ifagView =flagView.subviews[j];
                            if (ifagView.tag==1003||ifagView.tag==1004) {
                                ifagView.backgroundColor=[UIColor clearColor];
                            }else if (ifagView.tag==1001) {
                                ifagView.backgroundColor=[UIColor clearColor];;
                                ((UILabel*)ifagView).textColor=[UIColor whiteColor];
                                
                            }else if(ifagView.tag==1002){
                                SINAStuActivityTargetModel *targetModel = self.targetArray[i/2];
                                
                                NSString *format_time=@"00:00";
                                if (targetModel.startTime!=nil&&targetModel.startTime !=0) {
                                    NSString *currentTime =[SINACalendarUtils currentTimeStr];
                                    NSInteger seconds = ([currentTime integerValue]-targetModel.startTime )/1000;
                                    
                                    //format of hour
                                    NSString *str_hour = [NSString stringWithFormat:@"%02ld",seconds/3600];
                                    //format of minute
                                    NSString *str_minute = [NSString stringWithFormat:@"%02ld",(seconds%3600)/60];
                                    //format of second
                                    //                                    NSString *str_second = [NSString stringWithFormat:@"%02ld",seconds%60];
                                    //format of time
                                    format_time = [NSString stringWithFormat:@"用时%@时%@分",str_hour,str_minute];
                                    ((UILabel*)ifagView).text=format_time;
                                }
                            }else if (ifagView.tag==1005) {
                                ifagView.backgroundColor=[UIColor whiteColor];;
                                ((UIImageView*)ifagView).image=nil;
                            }
                        }
                    }else{
                        flagView.backgroundColor=[UIColor redColor];
                    }
                }
            }];
            //左滑删除
            UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(itemViewDeleteActivitySwipeFrom:)];
            [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
            [itemView addGestureRecognizer:recognizer];
            
            UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(itemViewDeleteActivitySwipeFrom:)];
            [rightRecognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
            [itemView addGestureRecognizer:rightRecognizer];
            
            [self.targetUiview addSubview:itemView];
            
            
            [[SINAUiviewClickLiserner alloc]viewClickFunction:deleteItemView andWithBlick:^(UIView *view) {
                if (itemView.frame.origin.x<0) {
                    DLog(@"删除");
                    if (!self.isUpdateItemLocation) {
                        self.isUpdateItemLocation=YES;
                        CGRect preFrame= itemView.frame;
                        if (preFrame.origin.x>=0) {
                            self.isUpdateItemLocation=NO;
                            return;
                        }
                        preFrame.origin.x=preFrame.origin.x+100;
                        [UIView animateWithDuration:0.45 animations:^{
                            itemView.frame=preFrame;
                        } completion:^(BOOL finished) {
                            self.isUpdateItemLocation=NO;
                            
                            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"确定删除小目标？" sureBtn:@"确定" cancleBtn:@"取消"];
                            alertView.resultIndex = ^(NSInteger index) {
                                [self deleteTargetFunction:targetModel];
                            };
                            [alertView showXLAlertView];
                            
                        }];
                    }
                }
            }];
        }
    }
}
//加载小目标
-(void)loadingTarGetDataFunction{
    DLog(@"加载小目标");
    [self.view endEditing:YES];

    [self startLoading];
    
    WeakSelf;
    [CXAFNHttpRequestUtils httpGetRequest:[NSString stringWithFormat:@"%@%@?organizeuuid=%@",baseUrl,userActivityTargetListUrl,self.model.activityUid] success:^(id responseData) {
        NSString *code = responseData[@"code"];
        [weakSelf stopLoading];
        if ([code isEqualToString:@"1000"]) {
            NSMutableArray *result = responseData[@"content"];
            if (result!=nil&&result.count>0) {
                [self.targetArray removeAllObjects];
                for (NSInteger i=0; i<result.count; i++) {
                    NSDictionary *dic = result[i];
                    SINAStuActivityTargetModel *targetModel=[[SINAStuActivityTargetModel alloc]initWithDict:dic];
                    [self.targetArray addObject:targetModel];
                }
                [self addTargetViewFunction];
            }else{
                [self.targetArray removeAllObjects];
                [self addTargetViewFunction];
            }
        }else{
            [self.targetArray removeAllObjects];
            if (self.isLoadingShow) {
                NSString *message =responseData[@"message"];
                WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定"
                                                                     cancleBtn:nil];
                [alertView showXLAlertView];
            }
            [self addTargetViewFunction];
            //无数据
        }
        
        
    } failure:^(NSError *error, NSInteger errCode) {
        [self.targetArray removeAllObjects];
        [self addTargetViewFunction];
        if (self.isLoadingShow) {
            //无数据
            [weakSelf stopLoading];
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"获取数据验证失败" sureBtn:@"确定"
                                                                 cancleBtn:@"取消"];
            [alertView showXLAlertView];
        }
        
    }];
}

//修改小目标时间
//userActivityUpdateTargetUrl
//flag 1 开始
//flag 2 结束小目标
-(void) updateTargetTime:(NSInteger) flag{
  
    [self startLoading];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    [dict setObject:self.targetModel.uuid forKey:@"uuid"];
    if (flag==1) {
        [dict setObject:[SINACalendarUtils currentTimeStr] forKey:@"starttime"];
    } else {
        [dict setObject:[SINACalendarUtils currentTimeStr] forKey:@"endtime"];
    }
    
    NSMutableDictionary *dict2 = [[NSMutableDictionary alloc]init];
    [dict2 setObject:[SINAHttpRequestUtils encodeDicParams:dict] forKey:@"sj"];
    WeakSelf;
    [CXAFNHttpRequestUtils httpPostRequest:[NSString stringWithFormat:@"%@%@",baseUrl,userActivityUpdateTargetUrl] withParamters:dict2 success:^(id responseData) {
        NSString *code = responseData[@"code"];
        
        [weakSelf stopLoading];
        
        if ([code isEqualToString:@"1000"]) {
            //添加成功
            if (flag==1) {
//                weakSelf.loadingView.text=@"开启请求成功";
                [YPXToastView showToastViewWithText:@"开启请求成功" andDuration:2 andCorner:5 andParentView:weakSelf.view];
                weakSelf.targetModel.targetFlag=1;
            } else {
//                weakSelf.loadingView.text=@"结束请求成功";
                [YPXToastView showToastViewWithText:@"结束请求成功" andDuration:2 andCorner:5 andParentView:weakSelf.view];
                weakSelf.targetModel=nil;
            }
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [weakSelf loadingTarGetDataFunction];
//                [weakSelf stopLoading];
                
            });
        }else{
            NSString *message =responseData[@"message"];
            WISAlertView *alertView =  [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定" cancleBtn:nil];
            [alertView showXLAlertView];
//            [weakSelf stopLoading];
        }
        
    } failure:^(NSError *error, NSInteger errCode) {
        DLog(@"加载 失败  %@ ",error.debugDescription);
        [weakSelf stopLoading];
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"添加失败 网络异常" sureBtn:@"确定" cancleBtn:nil];
        [alertView showXLAlertView];
    }];
}

- (void)itemViewDeleteActivitySwipeFrom:(UISwipeGestureRecognizer *)recognizer{
    
    
    UIView *currSwipeView =recognizer.view;
    
    
    if(recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
        DLog(@"左滑");
        if (!self.isUpdateItemLocation) {
            self.isUpdateItemLocation=YES;
            CGRect preFrame= currSwipeView.frame;
            
            if (preFrame.origin.x<0) {
                self.isUpdateItemLocation=NO;
                return;
            }
            preFrame.origin.x=preFrame.origin.x-100;
            [UIView animateWithDuration:0.45 animations:^{
                currSwipeView.frame=preFrame;
            } completion:^(BOOL finished) {
                self.isUpdateItemLocation=NO;
            }];
        }
        
    }else if(recognizer.direction == UISwipeGestureRecognizerDirectionRight){
        DLog(@"右滑");
        if (!self.isUpdateItemLocation) {
            self.isUpdateItemLocation=YES;
            CGRect preFrame= currSwipeView.frame;
            if (preFrame.origin.x>=0) {
                self.isUpdateItemLocation=NO;
                return;
            }
            preFrame.origin.x=preFrame.origin.x+100;
            [UIView animateWithDuration:0.45 animations:^{
                currSwipeView.frame=preFrame;
            } completion:^(BOOL finished) {
                self.isUpdateItemLocation=NO;
                
            }];
        }
    }
    
}

//SINAStuActivityTargetModel

-(void)deleteTargetFunction:(SINAStuActivityTargetModel*)targetModel{

    [self startLoading];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    [dict setObject:targetModel.uuid forKey:@"uuid"];
    NSMutableDictionary *dict2 = [[NSMutableDictionary alloc]init];
    [dict2 setObject:[SINAHttpRequestUtils encodeDicParams:dict] forKey:@"sj"];
    WeakSelf;
    [CXAFNHttpRequestUtils httpPostRequest:[NSString stringWithFormat:@"%@%@",baseUrl,userActivityTargetDeleteUrl] withParamters:dict2 success:^(id responseData) {
        NSString *code = responseData[@"code"];
        
        [weakSelf stopLoading];
        
        if ([code isEqualToString:@"1000"]) {
            //删除成功
//            self.loadingView.text=@"删除成功";
            
             [YPXToastView showToastViewWithText:@"删除成功" andDuration:2 andCorner:5 andParentView:weakSelf.view];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakSelf loadingTarGetDataFunction];
//                [weakSelf stopLoading];
            });
        }else{
            NSString *message =responseData[@"message"];
            WISAlertView *alertView =  [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定" cancleBtn:nil];
            [alertView showXLAlertView];
//            [weakSelf stopLoading];
        }
        
    } failure:^(NSError *error, NSInteger errCode) {
        [weakSelf stopLoading];
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"添加失败 网络异常" sureBtn:@"确定" cancleBtn:nil];
        [alertView showXLAlertView];
    }];
}

@end
